<template>
    <div class="box">
        <header class="header">
            <van-nav-bar
                title="编辑地址"
                left-text="返回"
                left-arrow
                @click-left="$router.back()"
            />
        </header>
        <div class="content">
           <van-address-edit
            :area-list="areaList"
            :address-info="AddressInfo"
            :value="110101"
            show-postal
            show-delete
            show-set-default
            show-search-result
            :search-result="searchResult"
            :area-columns-placeholder="['请选择', '请选择', '请选择']"
            @save="onSave"
            @delete="onDelete"
            @change-detail="onChangeDetail"
        >
        </van-address-edit>
        </div>
    </div>
</template>

<script>
import Vue from 'vue'
import { Area, Toast, AddressEdit } from 'vant'
import { areaList } from '@vant/area-data'
import { addressList, defaultAddress, deleteAddress, update, updateOrderAddress, addressAdd } from '../../api/order'

Vue.use(Area)
Vue.use(Toast)
Vue.use(AddressEdit)

export default {
  data() {
    return {
      addressid: '',
      areaList,
      searchResult: [],
      AddressInfo:{
          name:'ywc',
          tel: '',
          addressDetail: '',
          postalCode: '',
          isDefault: '',

      }
    };
  },
  mounted () {

      this.AddressInfo = this.$route.params.item

      var arr = this.AddressInfo.addressDetail.split('areaCode:')

      this.AddressInfo.addressDetail = arr[0]

      this.AddressInfo.areaCode = arr[1]

      this.AddressInfo.postalCode = arr[2]

  },
  methods: {

    // 点击保存时触发
    onSave(content) {
        Toast('编辑完成');
        // 通过默认地址接口查看默认地址是否存在
        defaultAddress({ userid: localStorage.getItem('userid') }).then(res => {
          // 默认地址不存在
          if(res.data.data.length === 0){
              //如果勾选了默认地址,更新地址
              if(content.isDefault === true){
                  content.addressDetail = content.addressDetail + 'areaCode:' + content.areaCode + 'areaCode:' + content.postalCode
                  update (content).then(res => {})
                  content.addressDetail = content.addressDetail.split('areaCode:')[0]
              }
              //如果没有勾选，更新地址
              else {
                  content.addressDetail = content.addressDetail + 'areaCode:' + content.areaCode + 'areaCode:' + content.postalCode
                  update (content).then(res => {})
                  content.addressDetail = content.addressDetail.split('areaCode:')[0]
              }
            // 默认地址存在
          } else {
            // 1 勾选默认 
            if(content.isDefault === true){
              // console.log(res.data.data);  
              res.data.data[0].isDefault = false  // 将原先的默认地址 isDefault 改为 false
              update (res.data.data[0]).then(res => {})  // 更新

              console.log(content);   // 将新修改的地址 isDefault 为 true 的信息 更新
              content.addressDetail = content.addressDetail + 'areaCode:' + content.areaCode + 'areaCode:' + content.postalCode
              update (content).then(res => {})
              content.addressDetail = content.addressDetail.split('areaCode:')[0]
            }
            // 2 未勾选默认
            else {
              // console.log(content);
              content.addressDetail = content.addressDetail + 'areaCode:' + content.areaCode + 'areaCode:' + content.postalCode
              update (content).then(res => {})
              content.addressDetail = content.addressDetail.split('areaCode:')[0]
            }
          }
        })
        this.$router.back({ name:'order2', params:{ content:content}})
    },

    // 删除地址
    onDelete() {
      Toast('delete');
      deleteAddress({addressid:this.AddressInfo.addressid}).then(res => {

      })
      this.$router.back()
    },

    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
};
</script>